//
//  RootViewController.m
//  Customer
//
//  Created by tangang on 2017/8/23.
//  Copyright © 2017年 tangang. All rights reserved.
//

#import "RootViewController.h"
#import "NavView.h"

@interface RootViewController ()<UIGestureRecognizerDelegate>
@property (nonatomic ,strong) NavView *navV;
@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [BaseVM manage].viewCtrl=self;
    self.view.backgroundColor = DEFAULTBGCOLOR;
    
    [self setupNavBar];

}

-(void)setupNavBar{
    //    [[UINavigationBar appearance]  setBackgroundImage:[[UIImage alloc] init] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    
    self.navigationController.navigationBar.tintColor = NAVCOLOR;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
//    [self.navigationController.navigationBar setBackgroundImage:[TGTools createImageWithColor:NAVCOLOR] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setBackgroundImage:[TGTools createImageWithColor:NAVCOLOR] forBarMetrics:UIBarMetricsDefault];
    
    
    [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:19],NSFontAttributeName,[UIColor whiteColor], NSForegroundColorAttributeName, nil];
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:0 target:self action:@selector(backClick)];
    self.navigationItem.leftBarButtonItem = backBtn;
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    UIImageView *line=[self getLineViewInNavigationBar:self.navigationController.navigationBar];
    line.hidden=YES;
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}


//找到导航栏最下面黑线视图
- (UIImageView *)getLineViewInNavigationBar:(UIView *)view
{
    if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
        return (UIImageView *)view;
    }
    for (UIView *subview in view.subviews) {
        UIImageView *imageView = [self getLineViewInNavigationBar:subview];
        if (imageView) {
            return imageView;
        }
    }
    return nil;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [BaseVM manage].viewCtrl=self;
    self.hidesBottomBarWhenPushed = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (void)showNavLine{
    WS(weakSelf);
    UIView *line=[[UIView alloc]init];
    line.backgroundColor=DEFAULTBGCOLOR;
    [self.view addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(weakSelf.view);
        make.height.mas_equalTo(@.7);
    }];
}

//导航栏右键点击事件
- (void)onclickNav{
    
    if (_navV) {
        [self.navV remove];
    }else{
        [self.navV show];
    }
//
//    [UIView animateWithDuration:5.2 animations:^{
//        [self.navV mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(self.view);
//        }];
//        [self.navV layoutIfNeeded];
//    }];
    
}

- (void)backClick{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (NavView *)navV{
    if (!_navV) {
        _navV=[[NavView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _navV.removeBlock = ^{
            _navV=nil;
        };
        [self.view addSubview:_navV];
//        [_navV mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.bottom.right.equalTo(self.view);
//            make.width.mas_equalTo(@(0));
//        }];
    }
    return _navV;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
